const db = require("../config/db");

// 课程查询接口逻辑
exports.listVideo = (req, res) => {
  // 获取前端数据
  let { category, page, size } = req.query;
  page = (page - 1) * size;
  // 查询课程列表
  const pageSql =
    "select * from video where del=0 and category=? order by id limit ?,?";

  // 查询课程总数的sql
  const totalsql =
    "select count(*) as total from video where del=0 and category=?";

  db.query(pageSql, [category, Number(page), Number(size)], (err, respage) => {
    if (err) {
      return res.send({ code: 1, message: err.message });
    }
    db.query(totalsql, category, (err, resTotal) => {
      if (err) {
        return res.send({ code: 1, message: err.message });
      }
      res.send({ code: 0, data: { list: respage, total: resTotal } });
    });
  });
};

// 课程修改接口
exports.updateVideoById = (req, res) => {
  let { title, price, id } = req.query;
  let sql = `update video set `;
  let arr = [];
  // 同时修改标题和价格
  if (title && price) {
    sql = sql += `title=?,price=? where id=?`;
    arr = [title, Number(price), Number(id)];
  } else if (title) {
    // 单独修改标题
    sql = sql + `title=? where id=?`;
    arr = [title, Number(id)];
  } else if (price) {
    // 单独修改价格
    sql = sql + `price=? where id=?`;
    arr = [price, Number(id)];
  }
  db.query(sql, arr, (err, results) => {
    if (err) {
      return res.send({ code: 1, message: err.message });
    }
    res.send({ code: 0, message: "修改成功" });
  });
};

// 课程删除接口
exports.deleteVideoById = (req, res) => {
  let { id } = req.query;
  let sql = `update video set del=1 where id=?`;
  db.query(sql, id, (err, results) => {
    if (err) {
      return res.send({ code: 1, message: err.message });
    }
    res.send({ code: 0, data: { message: "删除成功" } });
  });
};
